import {Component} from '@angular/core';
import {OrderHttpService} from "../../service/http/order.http.service";
import {HttpCustomConfig} from "../../service/base/base.http.service";
import * as dayjs from "dayjs";

@Component({
  selector: 'app-act-report',
  templateUrl: './act-report.component.html',
  styleUrls: ['./act-report.component.less']
})
export class ActReportComponent {
  dates: any[] = [];
  dataList: any[] = [];
  pageIndex: number = 1;
  pageSize: number = 10;
  pageTotal: number = 0;
  racerIdNo: string = '';
  racerName: string = '';
  defaultHttpConfig: HttpCustomConfig = {showLoading: false}
  exportHttpConfig: HttpCustomConfig = {showLoading: false}

  constructor(private orderHttpService: OrderHttpService) {
  }

  ngOnInit() {
    this.dates = [dayjs().startOf('date').toDate(), dayjs().endOf('date').toDate()];
    this.queryData();
  }

  queryDataInit(): void {
    this.pageIndex = 1;
    this.queryData();
  }

  queryData(): void {
    const startTime = dayjs(this.dates[0]).format('YYYY-MM-DD HH:mm:ss');
    const endTime = dayjs(this.dates[1]).format('YYYY-MM-DD HH:mm:ss');
    this.orderHttpService.queryRegister({
      startTime: startTime,
      endTime: endTime,
      pageIndex: this.pageIndex,
      pageSize: this.pageSize,
      racerIdNo: this.racerIdNo,
      racerName: this.racerName
    }, this.defaultHttpConfig).subscribe((res) => {
      this.dataList = res.data.dataList
      this.pageTotal = res.data.total;
    })
  }

  export(): void {
    const startTime = dayjs(this.dates[0]).format('YYYY-MM-DD HH:mm:ss');
    const endTime = dayjs(this.dates[1]).format('YYYY-MM-DD HH:mm:ss');
    this.orderHttpService.exportRegister({
      startTime: startTime,
      endTime: endTime,
      pageIndex: 1,
      pageSize: -1,
      racerIdNo: this.racerIdNo,
      racerName: this.racerName
    }, this.exportHttpConfig).subscribe((res) => {
      if (res.msg) {
        window.open(res.msg, '_blank');
      }
    })
  }

  downloadWork(workFile: string): void {
    if (workFile) {
      workFile.split(',').forEach(file => {
        window.open(file, '_blank');
      })
    }
  }
}
